package com.ricchieri.lazymodel;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.service.interf.IScheduleCharterService;

public class ScheduleCharterLazyDataModel extends LazyDataModel<ScheduleCharter> {

	private static final long serialVersionUID = 4593253494642335825L;

	private List<ScheduleCharter> scheduleCharters;
	private IScheduleCharterService scheduleCharterService;
	private ScheduleCharterFilter scheduleCharterFilter;
	
	public ScheduleCharterLazyDataModel(IScheduleCharterService scheduleCharterService, ScheduleCharterFilter scheduleCharterFilter) {
		super();
		this.scheduleCharterService = scheduleCharterService;
		this.scheduleCharterFilter = scheduleCharterFilter;
	}
	
    @Override
    public Object getRowKey(ScheduleCharter scheduleCharter) {
        return scheduleCharter.getId();
    }

    @Override
    public ScheduleCharter getRowData(String scheduleCharterId) {
        int id = Integer.valueOf(scheduleCharterId);

        for (ScheduleCharter scheduleCharter : scheduleCharters) {
            if(id == scheduleCharter.getId()) {
                return scheduleCharter;
            }
        }
        return null;
    }
    
	@Override
	public List<ScheduleCharter> load(int startingAt, int maxPerPage, String sortField, 
			SortOrder sortOrder, Map<String, String> filters) {
		
		scheduleCharterFilter.setSortField(sortField);
		scheduleCharterFilter.setSortOrder(sortOrder.toString());
		
        if(getRowCount() <= 0) {
            setRowCount(getScheduleCharterService().getAllScheduleCharterCountByFilter(scheduleCharterFilter));
        }
        
        if(getRowCount() == startingAt && getRowCount()  != 0) {
        	startingAt = startingAt - BusinessSettings.ROW_MAX_RESULT;
        }
        scheduleCharters = getScheduleCharterService().getAllScheduleCharterPaginatedByFilter(scheduleCharterFilter, startingAt);

        setPageSize(BusinessSettings.ROW_MAX_RESULT);
        
        return scheduleCharters;
	}

	public List<ScheduleCharter> getScheduleCharters() {
		return scheduleCharters;
	}

	public void setScheduleCharters(List<ScheduleCharter> scheduleCharters) {
		this.scheduleCharters = scheduleCharters;
	}

	public IScheduleCharterService getScheduleCharterService() {
		return scheduleCharterService;
	}

	public void setScheduleCharterService(
			IScheduleCharterService scheduleCharterService) {
		this.scheduleCharterService = scheduleCharterService;
	}

	public ScheduleCharterFilter getScheduleCharterFilter() {
		return scheduleCharterFilter;
	}

	public void setScheduleCharterFilter(ScheduleCharterFilter scheduleCharterFilter) {
		this.scheduleCharterFilter = scheduleCharterFilter;
	}

}
